
' TPopupMenu
Type TPopupMenu Extends TWidget
	' Init
	Method Init(nID:String, nAffectingWidget:TWidget, nX:Int, nY:Int, nWidth:Int, nHeight:Int)
		Self.InitWidget(nID, "", nX, nY, nWidth, nHeight) 
		Self.SetPosition(0, 0) 
		Self.SetPositionAbs(0, 0) 
		Self.SetAffectingWidget(nAffectingWidget) 
		Self.SetAlpha(0.75)
		Self.SetVisible(False) 
	End Method
	
	' SetText
	Method SetText(nText:String)
		Self.text = nText
	End Method
	
	' Update
	Method Update() 
		If TInputSystem.GetMouseHit(1) Or TInputSystem.GetMouseHit(2)
			Self.SetVisible(False) 
			
			' Erase mouse events
			TInputSystem.EraseMouseEvents()
			Self.gui.eraseMouseEvents = True
		EndIf
	End Method
	
	' Draw
	Method Draw()
		' Alpha
		'.SetAlpha gui.alpha * Self.alpha
		
		'.SetColor Self.r, Self.g, Self.b
		'DrawRect Self.rX, Self.rY, Self.rWidth, Self.rHeight
		
		'.SetColor Self.textR, Self.textG, Self.textB
		'DrawText Self.text, Self.rX + 15, Self.rY + 0
	End Method
	
	' Popup
	Method Popup()
		Self.PopupAt(TInputSystem.GetMouseX(), TInputSystem.GetMouseY())
	End Method
	
	' PopupAt
	Method PopupAt(posX:Int, posY:Int)
		If posX + Self.rWidth > Self.gui.root.rWidth
			posX = Self.gui.root.rWidth - Self.rWidth
		End If
		If posY + Self.rHeight > Self.gui.root.rHeight
			posY = Self.gui.root.rHeight - Self.rHeight
		End If
		
		Self.MoveTo(posX, posY)
		Self.SetVisible(True)
		Self.Focus()
		
		' Erase mouse events
		TInputSystem.EraseMouseEvents()
	End Method
	
	' SetAffectingWidget
	Method SetAffectingWidget(widget:TWidget)
		Super.SetAffectingWidget(widget) 
		
		For Local child:TWidget = EachIn Self.list
			child.SetAffectingWidget(widget)
		Next
	End Method
	
	' AddMenuEntry
	Method AddMenuEntry(nText:String, nFunc(widget:TWidget)) 
		Self.SetSizeAbs(-1, 24 * (Self.list.Count() + 1))
		
		' TODO: Change widget
		Local button:TButton = TButton.Create(Self.id + Self.list.Count(), nText) 
		button.SetAffectingWidget(Self.GetAffectingWidget())
		button.onClick = nFunc
		button.SetSize(1.0, 0)
		button.SetSizeAbs(0, 24) 
		button.SetAlpha(Self.alpha)
		button.SetPositionAbs(0, 24 * Self.list.Count() ) 
		Self.Add(button)
	End Method
	
	' OnGUIAvailable
	Method OnGUIAvailable()
		For Local child:TWidget = EachIn Self.list
			child.gui = Self.gui
			child.OnGUIAvailable()
		Next
	End Method
	
	' Create
	Function Create:TPopupMenu(nID:String, nAffectingWidget:TWidget = Null, nWidth:Int = 140, nHeight:Int = 0)
		Local widget:TPopupMenu = New TPopupMenu
		widget.Init(nID, nAffectingWidget, 0, 0, nWidth, nHeight)
		Return widget
	End Function
End Type

' TMenuBar
Type TMenuBar Extends TWidget
	' Init
	Method Init(nID:String, nText:String, nX:Int, nY:Int, nWidth:Int, nHeight:Int)
		Self.InitWidget(nID, nText, nX, nY, nWidth, nHeight)
	End Method
	
	' SetText
	Method SetText(nText:String)
		Self.text = nText
	End Method
	
	' Update
	Method Update() 
		
	End Method
	
	' Draw
	Method Draw()
		' Alpha
		.SetAlpha gui.alpha * Self.alpha
		
		.SetColor Self.r, Self.g, Self.b
		DrawRect Self.rX, Self.rY, Self.rWidth, Self.rHeight
	End Method
	
	' AddMenu
	Method AddMenu(nText:String, popupMenu:TPopupMenu)
		' TODO: Change widget
		Local button:TButton = TButton.Create(Self.id + Self.list.Count(), nText)
		button.SetAffectingWidget(button) 
		popupMenu.SetAffectingWidget(button)
		button.SetPopupMenu(popupMenu)
		button.onClick = TMenuBar.PopupSubMenu
		button.SetSize(0, 1.0)
		button.SetSizeAbs(button.GetLabel().textSizeX + 5, 0)
		button.SetAlpha(Self.alpha) 
		' TODO: Set correct position
		button.SetPositionAbs(0, 0)
		Self.Add(button)
	End Method
	
	' PopupSubMenu
	Function PopupSubMenu(widget:TWidget)
		If widget.GetPopupMenu() <> Null
			widget.GetPopupMenu().PopupAt(widget.GetScreenX(), widget.GetScreenY2())
		EndIf
	End Function
	
	' Create
	Function Create:TMenuBar(nID:String, nText:String, nWidth:Int = 0, nHeight:Int = 24)
		Local widget:TMenuBar = New TMenuBar
		widget.Init(nID, nText, 0, 0, nWidth, nHeight)
		Return widget
	End Function
End Type
